package com.ruoyi.system.service.estate.impl;

import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.estate.Repair;
import com.ruoyi.system.domain.estate.Resident;
import com.ruoyi.system.mapper.estate.EstateRepairMapper;
import com.ruoyi.system.mapper.estate.EstateResidentMapper;
import com.ruoyi.system.service.estate.IRepairService;

/**
 * 维修管理Service业务层处理
 */
@Service
@Primary
public class RepairServiceImpl implements IRepairService {
    @Autowired
    private EstateRepairMapper repairMapper;
    
    @Autowired
    private EstateResidentMapper residentMapper;

    /**
     * 查询维修列表
     * 
     * @param repair 维修信息
     * @return 维修集合
     */
    @Override
    public List<Repair> selectRepairList(Repair repair) {
        return repairMapper.selectRepairList(repair);
    }

    /**
     * 查询维修详细信息
     * 
     * @param repairId 维修ID
     * @return 维修信息
     */
    @Override
    public Repair selectRepairById(Long repairId) {
        return repairMapper.selectRepairById(repairId);
    }

    /**
     * 查询用户的维修信息列表
     * 
     * @param userId 用户ID
     * @return 维修信息集合
     */
    @Override
    public List<Repair> selectRepairListByUserId(Long userId) {
        Repair repair = new Repair();
        repair.setResidentId(userId);
        return repairMapper.selectRepairList(repair);
    }

    /**
     * 新增维修
     * 
     * @param repair 维修信息
     * @return 结果
     */
    @Override
    public int insertRepair(Repair repair) {
        // 生成报修编号
        if (StringUtils.isEmpty(repair.getRepairCode())) {
            String repairCode = generateRepairCode();
            repair.setRepairCode(repairCode);
        }
        
        // 设置报修人ID，如果前端没有传入，则使用当前登录用户关联的住户ID
        if (repair.getResidentId() == null) {
            Long userId = SecurityUtils.getUserId();
            Resident resident = residentMapper.selectResidentByUserId(userId);
            if (resident == null) {
                throw new ServiceException("您尚未关联住户信息，请先完善住户资料或联系管理员");
            }
            repair.setResidentId(resident.getResidentId());
            repair.setResidentName(resident.getResidentName());
            repair.setContactPhone(resident.getPhone());
        }
        
        // 设置基本字段
        repair.setCreateTime(new Date());
        repair.setCreateBy(SecurityUtils.getUsername());
        repair.setRepairTime(new Date());
        repair.setRepairStatus("0");
        repair.setDelFlag("0");
        repair.setStatus("0");
        
        return repairMapper.insertRepair(repair);
    }

    /**
     * 生成维修编号
     * 格式：WX + 年月日 + 4位序列号
     */
    private String generateRepairCode() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateStr = sdf.format(new Date());
        return "WX" + dateStr + String.format("%04d", System.currentTimeMillis() % 10000);
    }

    /**
     * 修改维修
     * 
     * @param repair 维修信息
     * @return 结果
     */
    @Override
    public int updateRepair(Repair repair) {
        repair.setUpdateTime(new Date());
        repair.setUpdateBy(SecurityUtils.getUsername());
        return repairMapper.updateRepair(repair);
    }

    /**
     * 分配维修人员
     * 
     * @param repair 维修信息
     * @return 结果
     */
    public int assignRepair(Repair repair) {
        repair.setRepairStatus("1"); // 已派单
        return repairMapper.updateRepair(repair);
    }

    /**
     * 完成维修
     * 
     * @param repair 维修信息
     * @return 结果
     */
    public int completeRepair(Repair repair) {
        repair.setRepairStatus("2"); // 已完成
        return repairMapper.updateRepair(repair);
    }

    /**
     * 评价维修服务
     * 
     * @param repair 维修信息
     * @return 结果
     */
    @Override
    public int evaluateRepair(Repair repair) {
        repair.setUpdateTime(new Date());
        repair.setUpdateBy(SecurityUtils.getUsername());
        repair.setEvaluationTime(new Date());
        repair.setRepairStatus("4");
        return repairMapper.updateRepair(repair);
    }

    /**
     * 批量删除维修
     * 
     * @param repairIds 需要删除的维修ID
     * @return 结果
     */
    @Override
    public int deleteRepairByIds(Long[] repairIds) {
        return repairMapper.deleteRepairByIds(repairIds);
    }

    /**
     * 删除维修信息
     * 
     * @param repairId 维修ID
     * @return 结果
     */
    @Override
    public int deleteRepairById(Long repairId) {
        return repairMapper.deleteRepairById(repairId);
    }

    /**
     * 取消报修
     * 
     * @param repairId 维修信息主键
     * @return 结果
     */
    @Override
    public int cancelRepair(Long repairId) {
        // 获取维修信息
        Repair repair = repairMapper.selectRepairById(repairId);
        
        if (repair == null) {
            return 0;
        }
        
        // 只有待处理状态的报修才能取消
        if (!"0".equals(repair.getRepairStatus())) {
            return 0;
        }
        
        // 修改为已取消状态，而不是设置删除标志
        repair.setRepairStatus("3"); // 3表示已取消
        
        // 设置更新者
        repair.setUpdateBy(SecurityUtils.getUsername());
        
        // 设置更新时间
        repair.setUpdateTime(new Date());
        
        // 记录日志
        System.out.println("取消报修: ID=" + repairId + ", 用户=" + SecurityUtils.getUsername());
        
        return repairMapper.updateRepair(repair);
    }

    /**
     * 提交报修
     * 
     * @param repair 维修信息
     * @return 结果
     */
    @Override
    public int submitRepair(Repair repair) {
        // 生成报修编号
        if (StringUtils.isEmpty(repair.getRepairCode())) {
            String repairCode = generateRepairCode();
            repair.setRepairCode(repairCode);
        }
        
        // 设置报修人ID，如果前端没有传入，则使用当前登录用户关联的住户ID
        if (repair.getResidentId() == null) {
            Long userId = SecurityUtils.getUserId();
            Resident resident = residentMapper.selectResidentByUserId(userId);
            if (resident == null) {
                throw new ServiceException("您尚未关联住户信息，请先完善住户资料或联系管理员");
            }
            repair.setResidentId(resident.getResidentId());
            repair.setResidentName(resident.getResidentName());
            repair.setContactPhone(resident.getPhone());
        }
        
        // 设置基本字段
        repair.setCreateTime(new Date());
        repair.setCreateBy(SecurityUtils.getUsername());
        repair.setRepairTime(new Date());
        repair.setRepairStatus("0");
        repair.setDelFlag("0");
        repair.setStatus("0");
        
        return repairMapper.insertRepair(repair);
    }
} 